package com.xinjian.jianba.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xinjian.jianba.dao.dwmid.UserRelationDao;
import com.xinjian.jianba.dto.UserRelationDto;
import com.xinjian.jianba.dto.UserRelationVo;
import com.xinjian.jianba.enity.UserRelationEntity;
import com.xinjian.jianba.service.UserRelationService;
import com.xinjian.jianba.utils.Utils;

@Service
public class UserRelationServiceImpl implements UserRelationService {

	@Autowired
	private UserRelationDao userRelationDao;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void black(UserRelationDto userRelationDto) {
		UserRelationEntity userRelationEntity = new UserRelationEntity();
		userRelationEntity.setUserId(Long.parseLong(userRelationDto.getUserId()));
		userRelationEntity.setRelationId(Long.parseLong(userRelationDto.getRelationId()));
		List<UserRelationEntity> list = userRelationDao.select(userRelationEntity);
		if (list.size() > 0) {
			userRelationDao.black(userRelationEntity);
			userRelationEntity.setUserId(Long.parseLong(userRelationDto.getRelationId()));
			userRelationEntity.setRelationId(Long.parseLong(userRelationDto.getUserId()));
			userRelationDao.blackThen(userRelationEntity);
		} else {
			Date date = new Date();
			userRelationEntity.setRelation("0");
			userRelationEntity.setAddTime(new Timestamp(date.getTime()));
			userRelationEntity.setTimeAdd(date);
			userRelationDao.insert(userRelationEntity);
			userRelationEntity.setUserId(Long.parseLong(userRelationDto.getRelationId()));
			userRelationEntity.setRelationId(Long.parseLong(userRelationDto.getUserId()));
			userRelationDao.blackThen(userRelationEntity);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void cancleBlack(UserRelationDto userRelationDto) {
		UserRelationEntity userRelationEntity = new UserRelationEntity();
		userRelationEntity.setUserId(Long.parseLong(userRelationDto.getUserId()));
		userRelationEntity.setRelationId(Long.parseLong(userRelationDto.getRelationId()));
		userRelationDao.cancleBlack(userRelationEntity);
		userRelationEntity.setUserId(Long.parseLong(userRelationDto.getRelationId()));
		userRelationEntity.setRelationId(Long.parseLong(userRelationDto.getUserId()));
		userRelationDao.cancleBlackThen(userRelationEntity);
	}

	@Override
	public int focus(UserRelationDto userRelationDto) {
		UserRelationEntity userRelationEntity = new UserRelationEntity();
		userRelationEntity.setUserId(Long.parseLong(userRelationDto.getRelationId()));
		userRelationEntity.setRelationId(Long.parseLong(userRelationDto.getUserId()));
		userRelationEntity.setRelation("0");
		List<UserRelationEntity> list = userRelationDao.select(userRelationEntity);
		if (list.size() > 0) {
			return 3;//无法关注;已被拉黑
		}
		int re;
		try {
			Date date = new Date();
			userRelationEntity.setUserId(Long.parseLong(userRelationDto.getUserId()));
			userRelationEntity.setRelationId(Long.parseLong(userRelationDto.getRelationId()));
			userRelationEntity.setAddTime(new Timestamp(date.getTime()));
			userRelationEntity.setTimeAdd(date);
			re = userRelationDao.focus(userRelationEntity);//1 关注成功
		} catch (DuplicateKeyException e) {
			userRelationEntity.setUserId(Long.parseLong(userRelationDto.getUserId()));
			userRelationEntity.setRelationId(Long.parseLong(userRelationDto.getRelationId()));
			userRelationEntity.setAddTime(null);
			userRelationEntity.setTimeAdd(null);
			userRelationEntity.setUserInfoEnity(null);
			List<UserRelationEntity> listRe = userRelationDao.select(userRelationEntity);
			if((listRe.size())>0){
				String relation=listRe.get(0).getRelation();
				if("0".equals(relation)){
					return 4;//已拉黑此人,无法再关注
				}else if("9".equals(relation)){
					//已被取消拉黑,可以再次关注
					userRelationDao.updateToFocus(userRelationEntity);
					return 1;//关注成功
				}
			}
				re = 2;//重复插入
			
		} 
		return re;
	}

	@Override
	public void cancleFocus(UserRelationDto userRelationDto) {
		UserRelationEntity userRelationEntity = new UserRelationEntity();
		userRelationEntity.setUserId(Long.parseLong(userRelationDto.getUserId()));
		userRelationEntity.setRelationId(Long.parseLong(userRelationDto.getRelationId()));
		userRelationDao.cancelFocus(userRelationEntity);
	}

	@Override
	public List<UserRelationEntity> getFocusByUserId(Long userId) {
		// TODO Auto-generated method stub
		List<UserRelationEntity> list = userRelationDao.getFocusByUserId(userId);
		return list;
	}

	@Override
	public List<UserRelationEntity> getBlackByUserId(Long userId) {
		// TODO Auto-generated method stub
		List<UserRelationEntity> list = userRelationDao.getBlackByUserId(userId);
		return list;
	}

	@Override
	public Long getBlackCountsByUserId(Long userId) {
		// TODO Auto-generated method stub
		HashMap<String, Long> map = userRelationDao.getBlackCountsByUserId(userId);
		Long counts = map.get("black_counts");
		return counts;
	}

	@Override
	public Long getFocusCountsByUserId(Long userId) {
		// TODO Auto-generated method stub
		HashMap<String, Long> map = userRelationDao.getFocusCountsByUserId(userId);
		Long counts = map.get("focus_counts");
		return counts;
	}

	@Override
	public Map<String, Object> getFocusAndCountsByUserId(Long userId) {
		List<UserRelationEntity> list = this.getFocusByUserId(userId);
		List<UserRelationVo> relist = new ArrayList<UserRelationVo>();
		Map<String, Object> map = new HashMap<String, Object>();
		Long count = this.getFocusCountsByUserId(userId);
		map.put("count", count + "");
		for (UserRelationEntity userRelationEntity : list) {
			UserRelationVo userRelationVo = new UserRelationVo();
			userRelationVo.setAddTime(Utils.convertTimestampToString(userRelationEntity.getAddTime()));
			//		userRelationVo.setCounts(count);
			userRelationVo.setNickName((userRelationEntity.getUserInfoEnity() == null ? "" : (userRelationEntity.getUserInfoEnity()
					.getNickname() == null ? "" : (userRelationEntity.getUserInfoEnity().getNickname()))));
			userRelationVo.setRelation(userRelationEntity.getRelation());
			userRelationVo.setRelationId(userRelationEntity.getRelationId());
			userRelationVo.setUserId(userRelationEntity.getUserId());
			userRelationVo.setUser_logo((userRelationEntity.getUserInfoEnity() == null ? "" : (userRelationEntity.getUserInfoEnity()
					.getUserLogo() == null ? "" : (userRelationEntity.getUserInfoEnity().getUserLogo()))));
			relist.add(userRelationVo);
		}
		map.put("result", relist);
		return map;
	}

	@Override
	public Map<String, Object> getBlacksAndCountsByUserId(Long userId) {
		List<UserRelationEntity> list = this.getBlackByUserId(userId);
		List<UserRelationVo> relist = new ArrayList<UserRelationVo>();
		Map<String, Object> map = new HashMap<String, Object>();
		Long count = this.getBlackCountsByUserId(userId);
		map.put("count", count);
		for (UserRelationEntity userRelationEntity : list) {
			UserRelationVo userRelationVo = new UserRelationVo();
			userRelationVo.setAddTime(Utils.convertTimestampToString(userRelationEntity.getAddTime()));
			//		userRelationVo.setCounts(count);
			userRelationVo.setNickName((userRelationEntity.getUserInfoEnity() == null ? "" : (userRelationEntity.getUserInfoEnity()
					.getNickname() == null ? "" : (userRelationEntity.getUserInfoEnity().getNickname()))));
			userRelationVo.setRelation(userRelationEntity.getRelation());
			userRelationVo.setRelationId(userRelationEntity.getRelationId());
			userRelationVo.setUserId(userRelationEntity.getUserId());
			userRelationVo.setUser_logo((userRelationEntity.getUserInfoEnity() == null ? "" : (userRelationEntity.getUserInfoEnity()
					.getUserLogo() == null ? "" : (userRelationEntity.getUserInfoEnity().getUserLogo()))));
			relist.add(userRelationVo);
		}
		map.put("result", relist);
		return map;
	}

}
